package com.company.Thread.Concurrent;

public class sync2 {
    int n = 0;
//    Lock l = new ReentrantLock();

    // AB两个进程想要的效果
    // A->B->A->B->........
    public static void main(String[] args) {
        sync2 s = new sync2();

        new Thread(()->{
            for(int i=0; i<10; i++) {
                try {
                    s.jumpin();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }, "A").start();

        new Thread(()->{
            for(int i=0; i<10; i++) {
                try {
                    s.jumpout();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }, "B").start();
    }


    public synchronized void jumpin() throws InterruptedException {
        if (n!=0) this.wait();
        n+=1;
        if (n==1) System.out.println("我进来了，怎么样");
        this.notifyAll();
    }

    public synchronized void jumpout() throws InterruptedException {
        if (n==0) this.wait();
        n-=1;
        if (n==0) System.out.println("我又出来了，打我阿");
        this.notifyAll();
    }
}
